package com.example.productserver.sentinel;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.example.productserver.exception.CustomException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 统一异常处理实现方式
 */
@Component
public class MyBlockExceptionHandler implements BlockExceptionHandler{
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        if (e instanceof FlowException) {

            throw new CustomException("===接口限流了===");

        } else if (e instanceof DegradeException) {

            throw new CustomException("===服务降级了===");

        } else if (e instanceof ParamFlowException) {

            throw new CustomException("===热点参数限流了===");

        } else if (e instanceof SystemBlockException) {
            throw new CustomException("===触发系统保护规则了===");


        } else if (e instanceof AuthorityException) {
            throw new CustomException("===授权规则不通过===");

        }
    }
}
